/**
 * Created by Administrator on 2017/10/13.
 */
$(function() {

    // 获取公告
    article.init();
    // 获取个性化设置
    setting.init();

    var $user = $('#userAccount');
    var $pass = $("#userPasswd");

    /*输入框获得焦点*/
    $('input[type="text"],input[type="password"]').focus(function(){
        $(this).siblings('.icon').addClass('click')
    })
    $('input[type="text"],input[type="password"]').blur(function(){
        $(this).siblings('.icon').removeClass('click')
    })

    $("#userPasswd,#userAccount").focus(function(){
        $('.right').removeClass('error');
    });

    $user.blur(function(){
        var $this = $(this);
        if($this.val() == ''){
            $(".msg").html('用户名不能为空！');
            $('.right').addClass('error');
            return;
        }else{
            $('.right').removeClass('error');
        }
    });

    $pass.blur(function(){
        var $this = $(this);
        if($this.val().length < 6 && $this.val() !== ''){
            $(".msg").html('密码不能小于6位！');
            $('.right').addClass('error');
            return;
        }else if( $this.val() == ''){
            $(".msg").html('密码不能为空！');
            $('.right').addClass('error');
            return;
        }else{
            $('.right').removeClass('error');
        }
    });
    $('#userAccount,#userPasswd,#verifyCode').keydown(function(e){
        var code = e.keyCode;
        if( code === 13 ){
            login();
        }
    });
    $('.tabs li').click(function(){
        var $this = $(this);
        $this.addClass('active').siblings('li').removeClass('active');
        $("#isSupportSso").val($.trim($this.attr("isSupportSso")));
    })
});

function login(){
    var $user = $('#userAccount');
    var $pass = $("#userPasswd");
    var $verify = $("#verifyCode");

    if($user.val() == ''){
        $(".msg").html('用户名不能为空！');
        $('.right').addClass('error');
        return;
    }else{
        $('.right').removeClass('error');
    }

    if($pass.val().length < 6 && $pass.val() !== ''){
        $(".msg").html('密码不能小于6位！');
        $('.right').addClass('error');
        return;
    }else if( $pass.val() == ''){
        $(".msg").html('密码不能为空！');
        $('.right').addClass('error');
        return;
    }else{
        //$pass.val(hex_sha256($pass.val()));
        $('.right').removeClass('error');
    }
    if($verify.val() == ''){
        $(".msg").html('验证码不能为空！');
        $('.right').addClass('error');
        return;
    }
    if( $('.right').hasClass('error') ){
        return;
    }
    var userAccount = $.trim($("#userAccount").val());
    var userPasswd = $.trim($("#userPasswd").val());
    var verifyCode = $.trim($("#verifyCode").val());
    var isSupportSso = $.trim($("#isSupportSso").val());
    var remeberMe = $("#remeberMe").prop('checked');
    var domain = $("#domain").val();
    // 通过域名登录的情况下，在登录之前需要检测该账户是否是该域名下单位的账户
    var checkUserAccount = check_user_account(userAccount);
    if(checkUserAccount){
        $('.right').addClass('error');
        return;
    }
    //var remeberMe = $("#remeberMe").prop('checked');alert(remeberMe);
    var params = {
        'userAccount': userAccount,
        'userPasswd': userPasswd,
        'verifyCode': verifyCode,
        'domain': domain,
        'isSupportSso': isSupportSso,
        'remeberMe': remeberMe
    };

    $.ajax({
        url: basePath + "signin",
        data: params,
        type: "post",
        dataType:"json",
        beforeSend: function (){
            $(".img-loading").css("display", 'block');
        },
        success: function(data) {
            var code = data.code;
            var message = data.message;
            if("10000" == code){
                window.location.href = basePath + "redirect";return;
            }
            if(message){
                $('.right').addClass('error');
                $(".msg").html(message);
                // 每次登录失败后刷新验证码
                changeVerifyCode();
            };
            $(".img-loading").css("display", 'none');
        },
        complete: function (XMLHttpRequest, textStatus) {
            $(".img-loading").css("display", 'none');
        }
    });
}

function check_user_account(userAccount) {
    var flag = true;
    var companyId = $("#companyId").val();
    if(!companyId) {
        $(".msg").html('域名无效，无法登录');
    }else{
        var param = {
            'userAccount': userAccount,
            'companyId': companyId
        };
        $.ajax({
            url: basePath + "domain/check/domain/userAccount",
            data: param,
            type: "post",
            async: false,
            dataType:"json",
            success: function(data) {
                var result = data.result;
                if(result){
                    var valid = result.isValid;
                    console.log(valid)
                    if(valid == "3"){
                        $(".msg").html('该账号正在审核中...');
                    }else{
                        flag = false
                    }
                }else{
                    $(".msg").html('该用户非本公司人员!');
                }
            }
        });
    }
    return flag;
}

/**
 * 注册
 */
function register() {
    window.open(basePath + "domain/register.htm?domain="+$("#domain").val()+"&companyId=" + $("#companyId").val());
}

function changeVerifyCode() {
    $.ajax({
        url: basePath + "popup/changeVerifyCode",
        data: {},
        type: "post",
        dataType:"json",
        success: function(data) {
            var base64Image = data.result.base64Img;
            if(base64Image){
                $("#verifyCodeImg").attr("src", "data:image/png;base64," + base64Image);
                setCookie("verifyResult",data.result.verifyResult);
            }
        }
    });
}
